﻿
using Microsoft.Web.Publishing.Tasks;
namespace TransformConfig.Workflow
{
    public class Transformation
    {
        public static void PerformTransform(string sourceFilename, string transformFilename, string outputFilename)
        {
            var xmlTarget = OpenSourceFile(sourceFilename);
            var flag = OpenTransformFile(transformFilename)
                        .Apply(xmlTarget);
            if (flag)
            {
                SaveTransformedFile(xmlTarget, outputFilename);
            }
        }

        private static XmlTransformableDocument OpenSourceFile(string filename)
        {
            var document = new XmlTransformableDocument { PreserveWhitespace = true };
            document.Load(filename);
            return document;
        }

        private static XmlTransformation OpenTransformFile(string filename)
        {
            return new XmlTransformation(filename);
        }

        private static void SaveTransformedFile(XmlTransformableDocument document, string destinationFile)
        {
            document.Save(destinationFile);
        }
    }
}
